package com.example.enjoyarkuix;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;

import com.example.enjoyarkuix.bean.ItemDataBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ohos.stage.ability.adapter.StageActivity;


/**
 * Example ace activity class, which will load ArkUI-X ability instance.
 * StageActivity is provided by ArkUI-X
 *
 * @see <a href=
 * "https://gitee.com/arkui-crossplatform/doc/blob/master/contribute/tutorial/how-to-build-Android-app.md">
 * to build android library</a>
 */
public class EntryCommonAbilityActivity extends StageActivity {
    String moduleName = "";
    String abilityName = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent == null) {
            Log.e("EntryCommonAbilityActivity", "intent==null: ");
            return;
        }

        String params = intent.getStringExtra("params");
        if (params != null) {
            getparam(params);
        } else {
            ItemDataBean itemDataBean = (ItemDataBean) intent.getSerializableExtra("ItemDataBean");
            moduleName = itemDataBean.getItemModuleName();
            abilityName = itemDataBean.getItemAbilityName();
        }

        Log.i("EntryCommonAbilityActivity", "=========: " + moduleName + ":" + abilityName + ":");
        setInstanceName("com.example.enjoyarkuix_ios:" + moduleName + ":" + abilityName + ":");
        super.onCreate(savedInstanceState);
    }

    private void getparam(String params) {
        Log.i("EntryCommonAbilityActivity", "params =========: " + params);
        try {
            JSONObject object = new JSONObject(params);
            params = object.getString("params");
            JSONArray array = new JSONArray(params);
            for (int i = 0; i < array.length(); i++) {
                object = array.getJSONObject(i);
                if ("moduleName".equals(object.getString("key"))) {
                    moduleName = object.getString("value");
                }
                if ("abilityName".equals(object.getString("key"))) {
                    abilityName = object.getString("value");
                }
            }
        } catch (JSONException e) {
        }
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }
}
